#include "enter_leave_widget.h"

#include "labelx.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QEvent>

EnterLeaveWidget::EnterLeaveWidget(QWidget *parent): QWidget{parent}
{
    // 垂直布局
    QVBoxLayout* verticalLayout = new QVBoxLayout(this);

    // 1. 自定义控件实现
    // 文本
    LabelX* labelx = new LabelX(this);
    labelx->setText("");
    labelx->setFrameShape(QFrame::Box);
    labelx->setFixedHeight(50);
    labelx->setAlignment(Qt::AlignCenter);
    labelx->setStyleSheet("background-color: blue; color: white; font-size: 25px");

    verticalLayout->addWidget(labelx);

    // 1. 事件过滤器实现
    // 文本
    label = new QLabel(this);
    label->setText("");
    label->setFrameShape(QFrame::Box);
    label->setFixedHeight(50);
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("background-color: red; color: white; font-size: 25px");

    verticalLayout->addWidget(label);

    // 安装事件过滤器
    label->installEventFilter(this);
}

static int count = 0;

/** 事件过滤
 * @brief EnterLeaveWidget::eventFilter
 * @param watched
 * @param event
 * @return
 */
bool EnterLeaveWidget::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::Enter) {
        label->setText(QString::number(count++));
    } else if(event->type() == QEvent::Leave) {
        label->setText(QString::number(count++));
    }

    return QWidget::eventFilter(watched, event);
}
